/**
 * 
 */
package net.mikoo.seals.agent.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * @author huangxm
 * 
 */
public class StringUtil {

	/**
	 * 将字符解析成数组 字符结构 
	 * key|value 
	 * 111111|bbbbbb 
	 * 222222|aaaaaa
	 * 
	 * @param item
	 * @return
	 */
	public static Map<String, Object> parsedToMap(String item) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isEmpty(item)) {
			return map;
		}

		String strs[] = item.split("\r\n");
		if(strs!=null && strs.length>0){
			
			for (String string : strs) {

				String str[] = string.toString().split("\\|");
				if (str == null || str.length != 2) {
					continue;
				}
				String key = str[0];
				String value = str[1];

				map.put(key, value);
			}
		}
		return map;
	}
	/**
	 * 格式aaaa,bbbb,cccc
	 * @param item
	 * @return
	 */
	public static List<String> parsedToArray(String item,String formate){
		List<String> list = new ArrayList<String>();
		if(StringUtils.isNotEmpty(item)){
			if(item.indexOf(formate)>=0){
				
				if(formate.equals("|")){
					formate = "\\"+formate;
				}
				String str[] = item.split(formate);
				if(str!=null && str.length>0){
					
					for (String string : str) {
						list.add(string);
					}
				}
			}else{
				
				list.add(item);
			}
		}
		
		
		return list;
	}
	
	
	/**
	 * @param strs
	 *        待转化字符串
	 * @return
	 * @author estone
	 * @description 下划线格式字符串转换成驼峰格式字符串
	 *              eg: player_id -> playerId;<br>
	 *              player_name -> playerName;
	 */
	public static String underScoreToCamelCase(String strs) {
		String[] elems = strs.split("_");
		for ( int i = 0 ; i < elems.length ; i++ ) {
			elems[i] = elems[i].toLowerCase();
			if (i != 0) {
				String elem = elems[i];
				char first = elem.toCharArray()[0];
				elems[i] = "" + (char) (first - 32) + elem.substring(1);
			}
		}
		StringBuffer sb = new StringBuffer();
		for ( String e : elems ) {
			sb.append(e);
		}
		return sb.toString();
	}

	/**
	 * @param param
	 *        待转换字符串
	 * @return
	 * @author estone
	 * @description 驼峰格式字符串 转换成 下划线格式字符串
	 *              eg: playerId -> player_id;<br>
	 *              playerName -> player_name;
	 */
	public static String camelCaseToUnderscore(String param) {
		Pattern p = Pattern.compile("[A-Z]");
		if (param == null || param.equals("")) {
			return "";
		}
		StringBuilder builder = new StringBuilder(param);
		Matcher mc = p.matcher(param);
		int i = 0;
		while (mc.find()) {
			builder.replace(mc.start() + i,mc.end() + i,"_" + mc.group().toLowerCase());
			i++;
		}
		if ('_' == builder.charAt(0)) {
			builder.deleteCharAt(0);
		}
		return builder.toString();
	}

	/**
	 * 生成随机数
	 *
	 * @Version 1.0
	 *
	 * @Author huangxiaoming
	 *
	 * @Email huangxm129@163.com
	 *
	 * @History
	 *
	 *<li>Author: huangxiaoming</li>
	 *<li>Date: 2016年1月3日</li>
	 *<li>Version: 1.0</li>
	 *<li>Content: create</li>
	 *
	 * @param charCount
	 * @return
	 */
	public static String getRandNum(int charCount) {
		String charValue = "";
		Random r = new Random();
		for (int i = 0; i < charCount; i++) {
			char c = (char) (0 + r.nextInt(10 - 0) + '0');
			charValue += String.valueOf(c);
		}
		return charValue;
	}

	
	public static String getString(Object obj){
		if(obj == null){
			return null;
		}
		
		return obj.toString();
	}

	
	public static String encode(String text) {
		char c;
		StringBuffer n = new StringBuffer();
		for (int i = 0; i < text.length(); i++) {
			c = text.charAt(i);
			switch (c) {
			case '&': {
				n.append("&amp;");
				break;
			}
			case '<': {
				n.append("&lt;");
				break;
			}
			case '>': {
				n.append("&gt;");
				break;
			}
			case '"': {
				n.append("&quot;");
				break;
			}
			case '\'': {
				n.append("&apos;");
				break;
			}
			default: {
				n.append(c);
				break;
			}
			}
		}
		return new String(n);
	}
	
	public static String decode(String text) {
		char c, c1, c2, c3, c4, c5;
		StringBuffer n = new StringBuffer();
		for (int i = 0; i < text.length(); i++) {
			c = text.charAt(i);
			if (c == '&') {
				c1 = text.charAt(i + 1);
				c2 = text.charAt(i + 2);
				c3 = text.charAt(i + 3);
				c4 = text.charAt(i + 4);
				c5 = text.charAt(i + 5);

				if (c1 == 'a' && c2 == 'm' && c3 == 'p' && c4 == ';') {
					n.append("&");
					i += 5;
				} else if (c1 == 'l' && c2 == 't' && c3 == ';') {
					n.append("<");
					i += 4;
				} else if (c1 == 'g' && c2 == 't' && c3 == ';') {
					n.append(">");
					i += 4;
				} else if (c1 == 'q' && c2 == 'u' && c3 == 'o' && c4 == 't' && c5 == ';') {
					n.append("\"");
					i += 6;
				} else if (c1 == 'a' && c2 == 'p' && c3 == 'o' && c4 == 's' && c5 == ';') {
					n.append("'");
					i += 6;
				} else
					n.append("&");
			} else
				n.append(c);
		}
		return new String(n);
	}
	public static void main(String args[]){
		
		/*List<String> lsit =  StringUtil.parsedToArray(",wqeq,qwe,weq,");
		for (String string : lsit) {
			System.out.println(string);
		}*/
		
		//System.out.println(camelCaseToUnderscore("aaaAWa"));
		//System.out.println(underScoreToCamelCase("aaa_aa_wa"));
		
	       // return from + r.nextInt(to - from);
		System.out.println(getRandNum(2));
		
	}

}
